﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ParangonBiz.Data;
using ParangonBiz.SiteMapDataProvider;
using ParangonBiz.Tools;

namespace ParangonBiz
{
    public partial class InfoList : System.Web.UI.Page
    {
        public int CurrentYear { get; set; }

        public SiteContentType CurrentContentType
        {
            get { return (SiteContentType)ViewState["CurrentContentType"]; }
            set { ViewState["CurrentContentType"] = value; }
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["type"]))
            {
                string sType = Request.QueryString["type"];
                int iType;

                if (Int32.TryParse(sType, out iType))
                {
                    CurrentContentType = SiteContentType.Get(iType);
                    CurrentYear = FillYears(iType);
                    FillMessages(null);
                }
            }
            else
            {
                Response.Redirect("/");
            }

            if (!String.IsNullOrEmpty(Request.QueryString["year"]))
            {
                string sYear = Request.QueryString["year"];
                int iYear;

                if (Int32.TryParse(sYear, out iYear))
                {
                    CurrentYear = iYear;
                }
            }
        }

        private void CreateBreadCrumbs()
        {

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!String.IsNullOrEmpty(Request.QueryString["type"]))
                {
                    string sType = Request.QueryString["type"];
                    int iType;

                    if (Int32.TryParse(sType, out iType))
                    {
                        CurrentContentType = SiteContentType.Get(iType);
                        CurrentYear = FillYears(iType);
                        FillMessages(null);
                    }
                }
                else
                {
                    Response.Redirect("/");
                }
                
                if (!String.IsNullOrEmpty(Request.QueryString["year"]))
                {
                    string sYear = Request.QueryString["year"];
                    int iYear;

                    if (Int32.TryParse(sYear, out iYear))
                    {
                        CurrentYear = iYear;
                        FillMessages(CurrentYear);
                    }
                }
                else if (!String.IsNullOrEmpty(Request.QueryString["Id"]))
                {
                    pnlYearMenu.Visible = false;

                    string sSiteContentId = Request.QueryString["Id"];
                    int iSiteContentId;

                    if (Int32.TryParse(sSiteContentId, out iSiteContentId))
                    {
                        ShowListItem(iSiteContentId);
                    }
                }
            }
        }
        private void FillMessages(int? year)
        {
            mvCompanyMessages.ActiveViewIndex = 0;

            if (CurrentContentType != null && CurrentContentType.SiteContentTypeId.HasValue)
            {
                lMainContentTitle.Text = String.Format("{0} ({1})", CurrentContentType.Title, CurrentYear);

                List<KeyValuePair<string, Uri>> nodes = new List<KeyValuePair<string, Uri>>();

                SiteMenuItem smi = SiteMenuItem.GetMenuItemParent(CurrentContentType.SiteContentTypeId.Value,
                    (int) EnumHelper.PageType.List);

                if (smi != null)
                {
                    nodes.Add(new KeyValuePair<string, Uri>(smi.Title,
                        new Uri(Request.Url,
                            String.Format("/Info.aspx?id={0}", smi.KeyId))));
                }

                if (year.HasValue)
                {

                    nodes.Add(new KeyValuePair<string, Uri>(CurrentContentType.Title,
                        new Uri(Request.Url,
                            String.Format("/InfoList.aspx?type={0}&year={1}", CurrentContentType.SiteContentTypeId.Value,
                                year.Value))));
                }
                else
                {
                    nodes.Add(new KeyValuePair<string, Uri>(CurrentContentType.Title,
                        new Uri(Request.Url,
                            String.Format("/InfoList.aspx?type={0}", CurrentContentType.SiteContentTypeId.Value))));
                }

                ((AxSiteMapDataProvider)SiteMap.Provider).Stack(nodes);

                List<SiteContent> scList = SiteContent.ListAllContent(CurrentContentType.SiteContentTypeId.Value, CurrentYear);
                rptMessages.DataSource = scList.OrderByDescending(t => t.CreateDate);
                rptMessages.DataBind();
            }
        }
        private void ShowListItem(int siteContentId)
        {
            SiteContent sc = SiteContent.Get(siteContentId);
            if (sc != null && CurrentContentType != null && CurrentContentType.SiteContentTypeId.HasValue)
            {
                mvCompanyMessages.ActiveViewIndex = 1;
                lMainContent.Text = sc.Content;
                lMainContentTitle.Text = sc.Title;
                lCreatedDate.Text = sc.CreateDate.HasValue ? sc.CreateDate.Value.ToLongDateString() : String.Empty;

                List<KeyValuePair<string, Uri>> nodes = new List<KeyValuePair<string, Uri>>();
                nodes.Add(new KeyValuePair<string, Uri>(CurrentContentType.Title, new Uri(Request.Url, String.Format("/InfoList.aspx?type={0}&year={1}", CurrentContentType.SiteContentTypeId.Value, CurrentYear))));
                nodes.Add(new KeyValuePair<string, Uri>(sc.Title, Request.Url));
                ((AxSiteMapDataProvider)SiteMap.Provider).Stack(nodes);

                rptFiles.DataSource = sc.FileAttachements;
                rptFiles.DataBind();
            }
        }

        private int FillYears(int typeCode)
        {
            IOrderedEnumerable<int> lYearList = SiteContent.ListAllContentYear(typeCode).OrderByDescending(i => i);
            rptSubMenu.DataSource = lYearList;
            rptSubMenu.DataBind();

            return lYearList.First();
        }

        protected string GetYearStatus(object oYear)
        {
            if (oYear is int)
            {
                int iYear = Convert.ToInt32(oYear);
                return iYear == CurrentYear ? "active" : string.Empty;
            }
            return string.Empty;
        }
    }
}